package labox.innovation.gameserver.network.clientpackets;

import labox.innovation.config.Config;
import labox.innovation.gameserver.datatables.ItemTable;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.MerchantUpdate;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.item.FuzeEconomicManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class MerchantRequest extends L2GameClientPacket {
	private static final Logger _log = LoggerFactory.getLogger(MerchantRequest.class.getName());
	
    private static final String _C__18_MERCHANTREQUEST = "[C] 18 MerchantRequest";
    /** 玩家买 */
    private final byte MERCHANT_BUY_TYPE = 0;
    /** 玩家卖 */
    private final byte MERCHANT_SELL_TYPE = 1;
    /** 更新成功 */
    private final byte MERCHANT_OPERATION_OK = 1;
    /** 更新失败 */
    private final byte MERCHANT_OPERATION_FAIL = 0;
    /** 类别 */
    private byte _type;
    /** 物品ID */
    private int _itemID;
    /** 物品objectID */
    private long _objectID;
    /** 买卖物品数量 */
    private int _count;
    /** 商店购买系数 */
	private float _storeBuyRate;
	/**	商店出售系数 */
	private float _storeSellRate;
    
    /**
     * 玩家买操作
     */
    private void doBuy() {
    	FuzePcInstance player =	getClient().getActiveChar();
    	_storeBuyRate = player.getTemplate().getBaseShopBuyRate();
		int karma = player.getKarma();  //-- 获取当前玩家身上的善恶值

		if (karma >= Config.KARMAREDSSET) {
			_storeBuyRate = (float)(Config.KARMABASE + karma)/Config.KARMABASE;
		}

    	
    	int buyPrice = Math.round(ItemTable.getInstance().getTemplate(_itemID).getBuyPrice() * _storeBuyRate * _count);  //-- 计算银元数
    	    	
    	if (player.getInventory().hasEnoughSilverMoney(FuzeEconomicManager.OPSTATE.TRADE.getValue(), player, buyPrice)) {  //-- 判断钱是否够
    		if (player.getInventory().validateCapacityByItemId(_itemID, _count)) {  //-- 判断背包是否已满   		
    			player.reduceMoneySilver(FuzeEconomicManager.OPSTATE.TRADE.getValue(), buyPrice, player, true);  //-- 扣钱
        		L2ItemInstance item = player.addItem(FuzeEconomicManager.OPSTATE.TRADE.getValue(), _itemID, _count, player, true);  //-- 买入物品
        		if (item != null) {
        			long objectId = item.getObjectId();
            		String itemName = item.getItemName();
            		int itemIconId = item.getItem().getItemIconId();
            		byte slot = (byte)item.getLocationSlot();  //-- 新槽位
            		player.sendPacket(new MerchantUpdate(MERCHANT_BUY_TYPE, objectId, _count, player.getInventory().getMoneySilver(), itemName, (byte)itemIconId, slot));  //-- 下发更新成功包
        		}
        		        		
        		return;
        	} else {
        		player.sendPacket(new SystemMessage(SystemMessageId.INVENTORY_IS_FULL));  //-- 背包满
        	}
		} else {
			player.sendPacket(new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA));  //-- 钱不够
		}
		
		player.sendPacket(new SystemMessage(SystemMessageId.NPC_TRADE_FAIL));  //-- NPC交易失败
    }
    
    /**
     * 玩家卖操作
     */
    private void doSell() {
    	FuzePcInstance player =	getClient().getActiveChar();    	
    	L2ItemInstance item = player.getInventory().getItemByObjectId(_objectID);
    	if (item == null) {
    		player.sendPacket(new SystemMessage(SystemMessageId.NPC_TRADE_FAIL));  //-- NPC交易失败
    		return;
    	}
    	
    	int currentItemCounts = item.getCount();
    	int itemID = item.getItemId();
    	String itemName = item.getItemName();
		int itemIconId = item.getItem().getItemIconId();
		
		byte slot = (byte)item.getLocationSlot();  //-- 原始槽位
		
    	if (_count > currentItemCounts) {
    		_count = currentItemCounts;
    	}
    	
    	_storeSellRate = player.getTemplate().getBaseShopSellRate();
		int karma = player.getKarma();  //-- 获取当前玩家身上的善恶值
		
		if (karma >= Config.KARMAREDSSET) {
			_storeSellRate = (float)(Config.KARMABASE - karma)/Config.KARMABASE;
		}

    	int sellPrice = Math.round(ItemTable.getInstance().getTemplate(itemID).getSellPrice() * _storeSellRate * _count);  //-- 计算卖价
    	if (player.destroyItem(FuzeEconomicManager.OPSTATE.TRADE.getValue(), _objectID, _count, player, true)) {  //-- 销毁物品
    		player.addMoneySilver(FuzeEconomicManager.OPSTATE.TRADE.getValue(), sellPrice, player, true);  //-- 增加银文数
    		player.sendPacket(new MerchantUpdate(MERCHANT_SELL_TYPE, _objectID, _count, player.getInventory().getMoneySilver(), itemName, (byte)itemIconId, slot));  //-- 下发更新成功包   		
    		return;
    	} else {
    		player.sendPacket(new SystemMessage(SystemMessageId.NPC_TRADE_FAIL));  //-- NPC交易失败
    	}
    }
    
    public MerchantRequest() {
    	
    }

	@Override
	protected void readImpl() {
		_type = readByte();
		_objectID = readLong();
		_itemID = readInt();
		_count = readInt();
	}

	@Override
	protected void runImpl() {
		switch (_type) {
		case MERCHANT_BUY_TYPE:
			doBuy();
			break;
		case MERCHANT_SELL_TYPE:
			doSell();
			break;
		default: 
			break;
		}
	}
	
	@Override
	public String getType() {
		return _C__18_MERCHANTREQUEST;
	}

}
